import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Dell
 */
public class DeleteEntry extends javax.swing.JPanel {
	JPanel mainPanel;
	Connection con;
	String entryName [],titleName = null;
	static String getDate = null;
	private int index = 0, getCode = 0;
	
    public DeleteEntry(JPanel panel) {
    	mainPanel = panel;
    	
    	try {
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/user", "user", "user");
            System.out.println("DB connected from DeleteEntry");
            
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM ENTRY");

            //Get the number of data from the table INVENTORY
            int indexNum = 0;
            while(rset.next()) {
                indexNum++;
            }
            stmt.close();

            entryName = new String[indexNum + 1];
            stmt = con.createStatement();
            rset = stmt.executeQuery("SELECT * FROM ENTRY");

            //Output all the INVENTORY names in the combo box
            int i = 1;
            entryName [0] = null;
            while(rset.next()) {
            	entryName [i] = rset.getString(2);
                i++;
            }

            stmt.close();
            
        } catch (SQLException ex) {
        	//Logger.getLogger(ConnectingDatabase.class.getName()).log(Level.SEVERE, null, ex);
        	System.out.println(ex.getMessage());
        }
        
        initComponents();
        backButton.addActionListener(new PanelSwitchToEntryMenu());
    }
    
    @SuppressWarnings("unchecked")
    private void initComponents() {

    	deleteTitleComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        title = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        venueLabel = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        backButton = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(650, 650));
        
        deleteTitleComboBox.setModel(new javax.swing.DefaultComboBoxModel(entryName));
        deleteTitleComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                titleComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Delete Entry");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        title.setFont(new java.awt.Font("Tahoma", 0, 14));
        title.setText("Tittle :");

        timeLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        venueLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        
        descriptionTextArea.setEditable(false);
        descriptionTextArea.setEnabled(true);
        

        deleteButton.setText("DELETE");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Time :");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Vanue :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText(" Description :");

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(5);
        jScrollPane1.setViewportView(descriptionTextArea);

        backButton.setText("BACK");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(title)
                            .addComponent(jLabel4))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(venueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                            .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                            .addComponent(deleteTitleComboBox, 0, 339, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))))
                .addGap(107, 107, 107))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteTitleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(title))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(venueLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteButton)
                    .addComponent(backButton))
                .addGap(83, 83, 83))
        );
    }
    
    
    private void titleComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	String entryTime = null, entryDescription = null, entryVenue = null;
    	index = 0;
    	
    	titleName = (String)deleteTitleComboBox.getSelectedItem();

        index = (int)deleteTitleComboBox.getSelectedIndex();
        try {
            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM ENTRY WHERE E_TITLE LIKE '" + titleName + "'");

            while(rset.next()) {
            	getCode = Integer.parseInt(rset.getString(1));
                entryTime = rset.getString(4);
                entryVenue = rset.getString(5);
                entryDescription = rset.getString(6);
            }

            stmt.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,"WARNING : " + e.getMessage(),
                "Putra Clinic Database",JOptionPane.WARNING_MESSAGE);
            System.out.println("ERROR => " + e.getMessage());
        }
        
        timeLabel.setText(entryTime);
        venueLabel.setText(entryVenue);
        descriptionTextArea.setText(entryDescription);
    }

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
        int indexControl = 0;
        
    	int Delete = JOptionPane.showConfirmDialog(this,"Are you sure you want to delete? ","Delete Entry",JOptionPane.YES_NO_OPTION);
        if(Delete == JOptionPane.YES_OPTION){
            try{
            	
                Statement stmt = con.createStatement();
                stmt.executeUpdate("DELETE FROM ENTRY WHERE E_NUM = " + getCode);
                
                stmt.close();
                
                //Inform the user that the inventory chosen has been deleted
                JOptionPane.showMessageDialog(this,titleName + " has been successfully deleted",
                    "Delete Entry",JOptionPane.INFORMATION_MESSAGE);
                
                //Remove the inventory name from the combo box
                indexControl = index;
                deleteTitleComboBox.removeItemAt(indexControl);
                
                //Remove the inventory name from the other GUI combo box
                indexControl = index;
                Edit.editTitleComboBox.removeItemAt(indexControl+1);
                indexControl = index;
                ViewEntry.viewTitleComboBox.removeItemAt(indexControl+1);
                
                deleteTitleComboBox.setSelectedIndex(0);
                descriptionTextArea.setText(null);
                timeLabel.setText(null);
                venueLabel.setText(null);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(this,"WARNING : "+ e.getMessage(),"Delete Entry",JOptionPane.WARNING_MESSAGE);

            }
        }
    }

    class PanelSwitchToEntryMenu implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			deleteTitleComboBox.setSelectedIndex(0);
			
			timeLabel.setText(null);
			descriptionTextArea.setText(null);
			venueLabel.setText(null);
			
			CardLayout c = (CardLayout)(mainPanel.getLayout());
			c.show(mainPanel, "EntryMenu");
		}
	}

    
    private javax.swing.JButton backButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel title;
    static javax.swing.JComboBox deleteTitleComboBox;
    private javax.swing.JLabel venueLabel;
}
